package com.jiangli.service.Search.Impl;

import com.jiangli.entity.Dish;
import com.jiangli.mapper.DishMapper;
import com.jiangli.service.Search.DishIndexService;
import com.jiangli.utils.Sreach.RedisKeyUtils;
import com.jiangli.utils.Sreach.SearchUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 菜品索引服务
 */
@Service
@Slf4j
public class DishIndexServiceImpl implements DishIndexService {

    @Autowired
    private GenericIndexService<Dish, Long> genericIndexService;

    @Autowired
    private DishMapper dishMapper;

    private static final int EXPIRE_HOURS = 24;

    /**
     * 系统启动初始化索引
     */
    @PostConstruct
    public void initDishIndex() {
        //查询数据库
        log.info("初始化索引");
        List<Dish> allDishes = dishMapper.selectAllAvailable();
        //创建索引
        genericIndexService.batchBuildIndex(
                allDishes,
                Dish::getId,
                RedisKeyUtils.DISH_INFO,
                RedisKeyUtils.DISH_INDEX,
                this::getDishKeywords,
                EXPIRE_HOURS
        );
    }

    /**
     * 构建单个菜品索引
     */
    public void buildDishIndex(Dish dish) {
        genericIndexService.buildIndex(
                dish,
                Dish::getId,
                RedisKeyUtils.DISH_INFO,
                RedisKeyUtils.DISH_INDEX,
                this::getDishKeywords,
                EXPIRE_HOURS
        );
    }

    /**
     * 删除菜品索引
     */
    public void deleteDishIndex(Long dishId) {
        genericIndexService.deleteIndex(dishId, RedisKeyUtils.DISH_INFO);
    }

    /**
     * 获取菜品关键词 TODO 获取菜品哪里关键词
     */
    public List<String> getDishKeywords(Dish dish) {

        List<String> nameKeys = SearchUtils.ikSegment(dish.getDishName());
//        List<String> pinyinKeys = SearchUtils.ikSegment(dish.getPinyin());
//        List<String> tagKeys = SearchUtils.ikSegment(dish.getTags() == null ? "" : dish.getTags());
//        nameKeys.addAll(pinyinKeys);
//        nameKeys.addAll(tagKeys);
        List<String> description = SearchUtils.ikSegment(dish.getDescription());
        List<String> price = SearchUtils.ikSegment(dish.getPrice().toString());
        List<String> merchant = SearchUtils.ikSegment(dish.getMerchantId().toString());
        nameKeys.addAll(description);
        nameKeys.addAll(price);
        return nameKeys.stream().distinct().collect(Collectors.toList());
    }

    // 委托查询方法
    public Set<String> getDishIdsByKeyword(String keyword) {
        return genericIndexService.queryEntityIds(RedisKeyUtils.DISH_INDEX, keyword);
    }
//    // 获取实体 TODO 用已有的方法解决
//    public Dish getDishFromCache(Long dishId) {
//
//        return genericIndexService.getEntityFromCache(RedisKeyUtils.DISH_INFO, dishId, Dish.class);
//    }
}

